using System;
using System.ComponentModel;
using System.Xml;
using JetBrains.ReSharper.Feature.Services.CodeCleanup;
using JetBrains.Util;

namespace Neovolve.Extensibility.ReSharper
{
    /// <summary>
    /// The <see cref="ValueTypeAliasFormatterDescriptor"/>
    /// class is used to describe the <see cref="ValueTypeAliasFormatter"/> 
    /// for the ReSharper code cleanup user interface.
    /// </summary>
    [Category("C#")]
    [DisplayName("Type alias conversion")]
    [CLSCompliant(false)]
    public class ValueTypeAliasFormatterDescriptor : CodeCleanupOptionDescriptor<TypeAliasFormat>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ValueTypeAliasFormatterDescriptor"/> class.
        /// </summary>
        public ValueTypeAliasFormatterDescriptor()
            : base("CSTypeAlias")
        {
        }

        /// <summary>
        /// Loads the specified profile.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <param name="element">
        /// The element.
        /// </param>
        public override void Load(CodeCleanupProfile profile, XmlElement element)
        {
            TypeAliasFormat option = TypeAliasFormat.Ignore;
            String elementValue = XmlUtil.ReadLeafElementValue(element, Name);

            if (String.IsNullOrEmpty(elementValue) == false
                && Enum.IsDefined(typeof(TypeAliasFormat), elementValue))
            {
                option = (TypeAliasFormat)Enum.Parse(typeof(TypeAliasFormat), elementValue);
            }

            profile.SetSetting(this, option);
        }

        /// <summary>
        /// Presents the specified profile.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <returns>
        /// A <see cref="string"/> instance.
        /// </returns>
        public override String Present(CodeCleanupProfile profile)
        {
            return profile.GetSetting(this).ToString();
        }

        /// <summary>
        /// Saves the specified profile.
        /// </summary>
        /// <param name="profile">
        /// The profile.
        /// </param>
        /// <param name="element">
        /// The element.
        /// </param>
        public override void Save(CodeCleanupProfile profile, XmlElement element)
        {
            TypeAliasFormat options = profile.GetSetting(this);

            XmlUtil.CreateLeafElementWithValue(element, Name, options.ToString());
        }
    }
}